<template>
    <el-table style="width: 100%; margin-bottom: 20px" row-key="id" :data="PermisstionArr" border>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="权限值" prop="code"></el-table-column>
        <el-table-column label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="操作">
            <!-- row:即为已有的菜单对象|按钮的对象的数据 -->
            <template #="{ row, $index }">
                <el-button type="primary" size="small" @click="addPermisstion(row)" :disabled="row.level == 4 ? true : false">{{row.level == 3 ? '添加功能': '添加菜单' }}</el-button>
                <el-button type="primary" size="small" @click="updatePermisstion(row)" :disabled="row.level == 1 ? true : false">编辑</el-button>
                <el-popconfirm :title="`你确定要删除${row.name}?`" width="260px" @confirm="deletePermisstion(row.id)">
                    <template #reference>
                        <el-button type="primary" size="small" :disabled="row.level == 1 ? true : false">删除</el-button>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
    <!-- 对话框组件:添加或者更新已有的菜单的数据结构 -->
    <el-dialog v-model="dialogVisible" :title="menuData.id?'修改':'新增'">
        <!-- 表单组件:收集新增与已有的菜单的数据 -->
        <el-form>
            <el-form-item label="名称">
                <el-input placeholder="请你输入菜单名称" v-model="menuData.name"></el-input>
            </el-form-item>
            <el-form-item label="权限">
                <el-input placeholder="请你输入权限数值" v-model="menuData.code"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="save">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
  </template>
      
  <script setup lang='ts'>
    import {ref,onMounted, reactive} from 'vue'
    import {reqAllPermisstion,reqAddOrUpdateMenu,reqRemoveMenu} from '@/api/acl/menu/index'
    import type {Permisstion,PermisstionResponseData} from '@/api/acl/menu/type'
    import { ElMessage } from 'element-plus';
    let dialogVisible = ref<boolean>(false)
    let PermisstionArr = ref<Permisstion[]>([])
    let menuData = reactive<any>({
      name:'',
      code:'',
      level:0,
      pid:0
    })
    onMounted(()=>{
      getHasPermisstion()
    })
    const getHasPermisstion = async ()=>{
      let res = await reqAllPermisstion()
      if(res.code==200){
        PermisstionArr.value = res.data
      }
    }
    // 添加
    const addPermisstion = (row:Permisstion)=>{
      Object.assign(menuData,{
        id:0,
        name:'',
        code:'',
        level:0,
        pid:0
      })
      dialogVisible.value = true
      //收集新增的菜单的level数值
      menuData.level = row.level + 1
      //给谁新增子菜单
      menuData.pid = row.id
    }
    // 更新
    const updatePermisstion = (row:Permisstion)=>{
      dialogVisible.value = true
      Object.assign(menuData,row)
    }
    //保存
    const save= async ()=>{
      let res:PermisstionResponseData = await reqAddOrUpdateMenu(menuData)
      if(res.code==200){
        ElMessage({type:'success',message:menuData.id?'修改成功':'新增成功'})
        dialogVisible.value = false
        getHasPermisstion()
      }else{
        ElMessage({type:'success',message:menuData.id?'修改失败':'新增失败'})
      }
    }
    // 删除
    const deletePermisstion= async (id:number)=>{
        let res = await reqRemoveMenu(id)
        if(res.code==200){
          getHasPermisstion()
          ElMessage({type:'success',message:'删除成功'})
        }
    }
  </script>
      
  <style lang='scss' scoped></style>